package table;

import java.awt.Dimension;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public abstract class AbstractTable {

	public static final int VINYL_VIEW = 1;
	public static final int BORROWED_VIEW = 2;
	public static final int RENTED_OUT_VIEW = 3;
	
	private JTable jTable = new JTable();
	private JScrollPane contentPane;
	
	private DefaultTableModel tableModel;
	private DefaultListSelectionModel listSelectionModel;
	
	public AbstractTable(JScrollPane contentPane) {
		this.jTable.setFillsViewportHeight(true);
		this.jTable.setPreferredScrollableViewportSize(new Dimension(800,200));
		
		this.contentPane = contentPane;
	}
	
	public void activate() {
		this.contentPane.setViewportView(jTable);
		this.requestFocus();
		
		((JPanel)this.contentPane.getParent()).revalidate();
	}
	
	public void deactivate() {
		this.contentPane.remove(jTable);
		
		((JPanel)this.contentPane.getParent()).revalidate();
	}
	
	
	protected void addRow(Object[] rowData) {
		this.tableModel.addRow(rowData);
	}
	
	protected void removeRow(int row) {
		this.tableModel.removeRow(row);
	}
	
	protected boolean selectAndEditCellAt(int row, int column, boolean clearCellText) {
		boolean success = this.jTable.editCellAt(row, column);
		
		if (success) {
			this.jTable.changeSelection(row, column, false, false);

			if (clearCellText) this.setValueAt("", row, column);
		}
		return success;
	}
	
	protected void clearSelection() {
		this.jTable.clearSelection();
	}
	
	protected void setTableRenderer(DefaultTableCellRenderer tableRenderer) {
		this.jTable.setDefaultRenderer(Object.class, tableRenderer);
	}
	
	protected void setTableModel(DefaultTableModel tableModel) {
		this.jTable.setModel(tableModel);
		
		this.tableModel = tableModel;
	}
	
	protected void setListSelectionModel(DefaultListSelectionModel listSelectionModel) {
		this.jTable.setSelectionModel(listSelectionModel);
		
		this.listSelectionModel = listSelectionModel;
	}
	
	public void addListSelectionListener(ListSelectionListener listener) {
		this.listSelectionModel.addListSelectionListener(listener);
	}

	protected void setColumnSelectionAllowed(boolean columnSelectionAllowed) {
		this.jTable.setColumnSelectionAllowed(columnSelectionAllowed);
	}
	
	protected void setRowSelectionAllowed(boolean rowSelectionAllowed) {
		this.jTable.setRowSelectionAllowed(rowSelectionAllowed);
	}
	
	protected void setValueAt(Object value, int row, int column) {
		this.jTable.setValueAt(value, row, column);
	}
	
	public Object getValueAt(int row, int column) {
		return this.jTable.getValueAt(row, column);
	}

	protected void setData(Object[][] data, String[] columnIdentifiers) {
		this.tableModel.setDataVector(data, columnIdentifiers);
	}
	
	
	public int getRowCount() {
		return this.jTable.getRowCount();
	}
	
	protected int getColumnCount() {
		return this.jTable.getColumnCount();
	}
	
	protected TableColumn getColumn(Object identifier) {
		return this.jTable.getColumn(identifier);
	}
	
	protected void updateTable() {
		this.tableModel.fireTableDataChanged();
	}
	
	protected void requestFocus() {
		this.jTable.requestFocus();
	}
	
	abstract protected void populate();
	abstract public int getTableID();
	abstract protected Object[] getDataAtRow(int row);
	
	abstract public int insertLastRowToDatabase();
	abstract public int deleteSelectedRowsFromDatabase();
	abstract public int updateRowInDatabase(int row);
	
	
	
}
